package cn.edu.xmu.oomall.comment.service;


import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.oomall.comment.dao.CommentDao;
import cn.edu.xmu.oomall.comment.dao.OrderItemDao;
import cn.edu.xmu.oomall.comment.dao.bo.Comment;
import cn.edu.xmu.oomall.comment.dao.bo.OrderItem;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(propagation = Propagation.REQUIRED)
@RequiredArgsConstructor
public class CommentService {
    private final CommentDao commentDao;
    private final OrderItemDao orderItemDao;


    /**
     * 创建评论
     *
     * @param orderItemId orderItem id
     * @param comment     评论
     * @param user        登录用户
     * @return 新comment对象，带id
     */
    public Comment createComment(Long orderItemId, Comment comment, UserDto user) {
        // 1. 获取订单项
        OrderItem orderItem = orderItemDao.findById(orderItemId);
        // 2. 创建评论（所有业务规则都在OrderItem中）
        return orderItem.createComment(comment, user);
    }


    /**
     * 回复评论
     *
     * @param shopId
     * @param comment 回复
     * @param user    登录用户
     * @return 新comment对象，带id
     * @Param id 评论Id
     */
    public Comment createReply(Long id, Long shopId, Comment comment, UserDto user) {
        Comment reply = this.commentDao.findById(id);
        return reply.createReply(shopId, comment, user);
    }

    /**
     * 追评
     *
     * @param comment 回复
     * @param user    登录用户
     * @return 新comment对象，带id
     * @Param id 评论Id
     */
    public Comment addAdditionalComment(Long id, Comment comment, UserDto user) {
        Comment addtionalComment = this.commentDao.findById(id);
        return addtionalComment.addAdditionalComment(comment, user);
    }

    /**
     * 审核评论
     *
     * @param id
     * @param user
     */
    public void auditComment(Long id, UserDto user) {
        Comment comment = this.commentDao.findById(id);
        comment.auditComment(user);
    }

    /**
     * 隐藏评论
     *
     * @param id
     * @param user
     */
    public void hideComment(Long id, UserDto user) {
        Comment comment = this.commentDao.findById(id);
        comment.hideComment(user);
    }
}
